package cn.yedong.org.controller;

import cn.yedong.org.query.EmployeeQuery;
import cn.yedong.basic.util.AjaxResult;
import cn.yedong.basic.util.QueryResult;
import cn.yedong.org.domain.Employee;
import cn.yedong.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@ResponseBody
//@Controller
@Api(value="部门接口类",description = "处理部门的CRUD操作")
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    //注入部门业务的接口
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询单个对象
     * @param id
     * @return
     */
    //前端： get http://localhost:8080/employee/1
    @ApiOperation("接口：查询单个对象")
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable("id") Long id) {
        return employeeService.loadById(id);
    }

    /**
     * 查询所有
     * @return
     */
    //前端 get http://localhost:8080/employee
    @ApiOperation("接口：查询所有")
    @GetMapping
    public List<Employee> findAll() {
        return employeeService.loadAll();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    //前端： delete http://localhost:8080/employee/1
    @ApiOperation("接口：删除")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            employeeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 添加或修改
     * @param employee
     * @return
     */
    //前端 put http://localhost:8080/employee json对象
    @ApiOperation("接口：添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 分页查询
     * @param employeeQuery
     * @return
     */
    //前端 post http://localhost:8080/employee json对象
    @ApiOperation("接口：分页查询")
    @PostMapping
    public QueryResult findPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.pageQuery(employeeQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    //前端： delete http://localhost:8080/employee/1
    @ApiOperation("接口：批量删除")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        try {
            employeeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }
}
